<template>
  <lay-layer
    title="编辑API"
    v-model="visible"
    :shade="true"
    :shadeClose="false"
    :shadeOpacity="0.3"
    :maxmin="true"
    :area="['400px']"
    :teleportDisabled="false"
    :btn="actionBtn"
  >
    <Loading :loading="isLoading">
      <lay-form
        :model="formState"
        ref="formRef"
        size="xs"
        style="width: 88%; margin-top: 15px"
      >
        <lay-form-item label="API名称" prop="name" required>
          <lay-input
            type="text"
            v-model="formState.name"
            placeholder="API名称"
            allow-clear
          />
        </lay-form-item>

        <lay-form-item label="上级API" prop="pid" required>
          <lay-tree-select
            v-model="formState.pid"
            :data="apiList"
            placeholder="请选择"
            :search="true"
            style="width: 100%"
            :default-expand-all="true"
          >
            <template #title="{ data }">
              {{ data.name }}
            </template>
          </lay-tree-select>
        </lay-form-item>

        <lay-form-item label="API路径" prop="url" required>
          <lay-input
            type="text"
            v-model="formState.url"
            placeholder="例：/index/index"
            allow-clear
          />
        </lay-form-item>

        <lay-form-item label="排序" prop="sort" required>
          <lay-input-number v-model="formState.sort" placeholder="排序值" allow-clear />
        </lay-form-item>
      </lay-form>
    </Loading>
  </lay-layer>
</template>
<script setup>
import { ref, nextTick } from "vue";
import { msgSuccess, msgError } from "@/utils/message";
import {
  one_assign,
  // deepCopy,
  addDisabledToNode,
} from "@/utils/tool";
import * as Api from "@/api/menu/api";

// 定义抛出的事件名称
const emit = defineEmits(["callback"]);
const isLoading = ref(false);
const formRef = ref(undefined);
const visible = ref(false);
// 定义表单信息
const formState = ref({
  name: "",
  pid: 0,
  url: "",
  sort: 100,
});
const actionBtn = ref([
  {
    text: "提交",
    callback: () => {
      handleSubmit();
    },
  },
  {
    text: "取消",
    callback: () => {
      visible.value = false;
    },
  },
]);
// 权限列表
const apiList = ref([]);
// 当前信息
const detail = ref(undefined);

// 控制显示
const show = (api_list, data) => {
  visible.value = true;
  detail.value = data;

  const new_apiList = [
    {
      id: 0,
      pid: 0,
      name: "顶级API",
      title: "顶级API",
    },
    ...api_list,
  ];
  apiList.value = addDisabledToNode(new_apiList, detail.value["id"]);

  //初始化表单信息
  nextTick(() => {
    formState.value = one_assign(formState.value, detail.value, true);
  });
};

// 提交
const handleSubmit = () => {
  if (formState.value.name == "" || formState.value.name == null) {
    return msgError("API名称不能为空");
  }
  if (formState.value.pid === "" || formState.value.pid === null) {
    return msgError("请选择API上级");
  }
  if (formState.value.url === "" || formState.value.url === null) {
    return msgError("API路径不能为空");
  }
  if (formState.value.sort == "") {
    return msgError("排序值必填");
  }

  isLoading.value = true;
  Api.edit({ apiId: detail.value.api_id, form: formState.value })
    .then((result) => {
      if (result.status == 200) {
        msgSuccess(result.message);
        visible.value = false;
      } else {
        msgError(result.message);
      }
    })
    .finally(() => {
      emit("callback", "");
      isLoading.value = false;
    });
};

// 导出方法/属性给父组件
defineExpose({ show });
</script>
